/**
 * Created by chenqinghui on 2018/1/24.

 <Editor type="news" dispatch={this.props.dispatch} updateEditorContent={content} id="content" height="500" />
 type--models的namespace
 updateEditorContent--修改时传入富文本的内容
 *
 */

import React from 'react';
import { Link } from 'dva/router';
import { connect } from 'dva';
import PropTypes from 'prop-types';

import ueditorConfig from './resources/ueditor.config.js';
import ueditorAll from './resources/ueditor.all.js';


class Editor extends React.Component {
  constructor(props){
    super(props);
    this.state = {
    };
  }

  componentDidMount(){
    this.initEditor();
  }
  componentWillUnmount() {
    // 组件卸载后，清除放入库的id
    UE.delEditor("content");
  }

  componentWillReceiveProps(props){


    //var updateEditorContent = this.props.updateEditorContent;
    //console.log(updateEditorContent);
    //if(updateEditorContent!=""){
      //this.ueEditor.addListener('ready', function() {
      //  console.log('111111');
      //
      //
      //  this.ueEditor.setContent(updateEditorContent);
      //}.bind(this));


    //}

  }





  initEditor() {

    const id = "content";
    this.ueEditor = UE.getEditor("content", {/*这里是配置*/ });
    const self = this;
    this.ueEditor.ready((ueditor) => {
      if (!ueditor) {
        UE.delEditor(id);
        self.initEditor();
      }
      setTimeout(()=>{this.ueEditor.setContent(this.props.updateEditorContent);},500);
    });

    this.ueEditor.addListener('selectionchange', function(type) {
      var editorContent = this.ueEditor.getContent();
      this.props.dispatch({
        type: this.props.type + "/save",
        payload: {'content': editorContent}
      })
    }.bind(this));



  }

  //getContent(){
  //
  //  //UE.Editor.addListener( 'selectionchange', function( content ) {
  //  //  console.log('选区发生改变');});
  //  var content = UE.getContent();
  //  console.log("____--",content);
  //
  //};





  render(){

    return (
      <div>
        <div key="editor" id="content" name="content" type="text/plain"></div>
      </div>
    )
  }
}
export default Editor;
